package demo1;

import java.util.List;

public class MySingleList {
    static class ListNode {
        public int val;
        // 指向链表中下一个节点的引用
        public ListNode next;//ListNode 节点类型

        public ListNode(int val) {
            this.val = val;
        }

    }

    public ListNode head;//表示当前头节点

    public void createList() {

        ListNode node1 = new ListNode(12);
        ListNode node2 = new ListNode(23);
        ListNode node3 = new ListNode(34);
        ListNode node4 = new ListNode(45);
        ListNode node5 = new ListNode(56);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;

        this.head = node1;

    }
    //遍历列表
    public void display(){
        ListNode cur = head;//为了防止头节点位置改变
        while(cur != null){ //cur==null表示遍历完节点了
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }
    //得到单列表的长度
    public int size(){
        int count = 0;
        ListNode cur = head;//为了防止头节点位置改变
        while(cur != null){ //cur==null表示遍历完节点了
            count++;
            cur = cur.next;
        }
        //System.out.println(count);
        return count;
    }
    //查找是否包含关键字key在单链表中
    public boolean contains(int key){
        ListNode pos = head;
        while(pos != null){
            if((pos.val-key)==0){
                return true;
            }
            pos = pos.next;
        }
        return false;
    }

    // 1、无头单向非循环链表实现
    public class SingleLinkedList {
    }


    //头插法
    public void addFirst(int data){
        ListNode node = new ListNode(data);
        node.next = head;
        head = node;
    }

    //尾插法
    //cur==null 将链表的每个节点都遍历完了
    //cur.next==null cur现在是最后一个节点的位置
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if (head == null) {
            // 如果链表为空，新节点成为头节点
            head = node;
            return;
        }
        ListNode cur = head;
        // 遍历链表直到最后一个节点
        while (cur.next != null) {
            cur = cur.next;
        }
        // 将新节点添加到链表末尾
        cur.next = node;
    }

    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        if(index < 0 || index > size()) {
            System.out.println("index 不合法");
            return;
        }
        if(index == 0) {
            addFirst(data);
            return;
        }
        if(index == size()) {
            addLast(data);
            return;
        }
        ListNode cur = findIndexSubOne(index);
        ListNode node = new ListNode(data);
        node.next = cur.next;
        cur.next = node;
    }

    //找到要删除节点位置的前一个节点
    private ListNode findIndexSubOne(int index) {
        ListNode cur = head;
        while (index -1 != 0) {
            cur = cur.next;
            index--;
        }
        return cur;
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key) {
        ListNode cur = searchPrey(key);
        if(cur == null){
            System.out.println("没有你要删除的数字");
            return;
        }
        ListNode  del = cur.next;
        cur.next = del.next;
    }

    private ListNode searchPrey (int key){
        ListNode cur = head;
        while(cur.next != null){
            if(cur.next.val == key){
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }
    //删除所有值为key的节点
    public void removeAll(int key){
        if(head == null){
            return;
        }
        ListNode cur = head.next;
        ListNode prev = head;
        while(cur != null){
            if(cur.val == key) {
                prev.next = cur.next;
                cur = cur.next;
            }
            else{
                prev = cur;
                cur = cur.next;

            }
        }
        while(head.val == key){
            head = head.next;
        }
    }
    //清空列表
    public void clear(){
        head = null;//后面的节点就无法被引用了
    }

    //翻转列表
    public ListNode reverseList() {
        if(head == null){
            return null;
        }
        if(head.next == null){
            return head;
        }
        ListNode cur = head.next;
        head.next = null;
        while(cur != null){
            ListNode curNext = cur.next;
            cur.next = head;
            head = cur;
            cur = curNext;
        }
        return head;

    }

    //给定一个带有头节点head的费控单链表，返回链表的中间结点
    // 如果有两个中间结点，则返回第二个中间结点
    public ListNode middleNode(){
        /*if(head == null){
            return null;
        }*/
        ListNode fast = head;
        ListNode slow = head;
        while(fast !=null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }

    //输入一个链表，输出该链表中倒数第k个结点
    //fast先走k-1步 再一起走 fast走到最后一个结点时 slow所指的就是倒数第k个
    public ListNode findKthTOTail(int k ){
        ListNode fast = head;
        ListNode slow = head;
        if(k < 0 || head == null){
            return null;
        }
        for(int i = 0; i < k-1; i++){
            fast = fast.next;
            if(fast == null){
                return null;
            }
        }
        while(fast.next != null){
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }

    //将两个有序链表合并为一个新的有序链表并返回
    // 新链表时通过拼接给定的两个链表节点组成的
/*    public ListNode mergeTwoLists(ListNode headA,ListNode headB){
       *//* MySingleList A = new MySingleList();
        A.createList();
        MySingleList B = new MySingleList();
        B.createList();
        B.addIndex(0,0);
        B.addIndex(2,5);*//*
      *//*  ListNode headA = A.head;
        ListNode headB = B.head;*//*
        while(headA !=null &&headB != null){
            if(A.head.val>B.head.val){
                pHead.next = headB;
                pHead = pHead.next;
                headB = headB.next;
            }else{
                pHead.next = headA;
                pHead = pHead.next;
                headA = headA .next;
            }
        }

    }*/


        //编写代码 以给定值X为基准将链表分割为两部分
    // 所有小于x的节点排在大于或等于x的节点之前

    public ListNode partition(ListNode pHead, int x) {
        // write code here
        ListNode bs = null;// 小于x的链表的头节点（begin smaller）
        ListNode be = null;//小于x的链表的尾节点（end smaller）
        ListNode as = null;//大于等于x的链表的头节点（begin larger or same）
        ListNode ae = null;// 大于等于x的链表的尾节点（end larger or same）

        ListNode cur = pHead;
        //没有遍历完 整个链表
        while(cur != null) {
            if(cur.val < x) {
                //第一次插入
                if(bs == null) {
                    bs = be = cur;
                }else {
                    be.next = cur;// 将当前节点接到小于x的链表的尾部
                    be = be.next;// 更新小于x的链表的尾节点
                }
            }else {
                //第一次插入
                if(as == null) {
                    as = ae = cur;
                }else {
                    ae.next = cur;
                    ae = ae.next;
                }
            }
            cur = cur.next;
        }
        // 连接两个链表
        //第一个段 没有数据
        if(bs == null) {
            return as;
        }
        be.next = as;//// 小于x的链表的尾节点指向大于等于x的链表的头节点
        //防止 最大的数据 不是最后一个
        if(as!=null) {
            ae.next = null; // 确保大于等于x的链表的尾节点指向null，防止成环
        }
        return bs;
    }

    //链表的回文结构
    public boolean chkPalindrome(ListNode head) {
        // 1. 找到中间位置
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        //2. 开始翻转
        ListNode cur = slow.next;
        while(cur != null) {
            ListNode curNext = cur.next;//记录一下下一个节点
            cur.next = slow;
            slow = cur;
            cur = curNext;
        }
        //3. 此时翻转完成，开始判断是否回文
        while(head != slow) {
            if(head.val != slow.val) {
                return false;
            }
            if(head.next == slow) {//偶数情况
                return true;
            }
            head = head.next;
            slow = slow.next;
        }
        return true;
    }

    public void createLoop() {
        ListNode cur = head;
        while (cur.next != null) {
            cur = cur.next;
        }
        //cur.next == null;

        cur.next = head.next;
    }

    //判断是否有环
    public boolean hasCycle() {
        if(head == null) return false;
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if(fast == slow) {
                return true;
            }
        }
        return false;
    }

    // 返回入口点
    public ListNode detectCycle() {
        if(head == null) return null;
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null) { //有环
            fast = fast.next.next;
            slow = slow.next;
            if(fast == slow) { //相遇则跳出循环，因为这意味着链表中存在环。
                break;
            }
        }
        if(fast == null || fast.next == null) { //没环
            return null;
        }
        fast = head;//将 fast 指针重新设置为链表头部 head。
        while(fast != slow) {
            fast = fast.next;//当 fast 和 slow 再次相遇时（由于它们都以相同的速度移动
                             //并且都在环内，所以它们一定会再次相遇），相遇点就是环的起始节点。
            slow = slow.next;
        }
        return fast;
    }

    //删除链表中重复的节点
    public ListNode deleteDuplication(ListNode pHead) {
        ListNode cur = pHead;
        ListNode newHead = new ListNode(-1);
        ListNode tmpHead = newHead;
        //遍历链表的每个节点
        while(cur != null) {
            if(cur.next != null && cur.val == cur.next.val) {
                //一直让cur走到不重复的节点 然后把这个节点 加入到 不重复的链表当中
                while(cur.next != null && cur.val == cur.next.val) {
                    cur = cur.next;
                }
                cur = cur.next;
            }else {
                tmpHead.next = cur;
                tmpHead = tmpHead.next;
                cur = cur.next;
            }
        }
        tmpHead.next = null;
        return newHead.next;
    }


   /* *//**
     * 讲解栈的时候  写的例子
     *//*
    public void reversePrintList() {
        Stack<ListNode> stack = new Stack<>();
        ListNode cur = head;
        while (cur != null) {
            stack.push(cur);
            cur = cur.next;
        }

        while (!stack.isEmpty()) {
            ListNode top = stack.pop();
            System.out.print(top.val+" ");
        }
        System.out.println();

    }*/
}
